package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.PkGroupCreateActivity;
import reduce.fz.com.reduce.activity.PkVideoListActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.PkGroup;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.event.PkGroupCreateEvent;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.HttpUtils;

public class HotPkGroupFragment extends BaseListFragment<HotPkGroupFragment.HotPkGroupViewHolder, PkGroup> {
    /**
     * 0普通 1热门
     */
    private int type = 0;

    public HotPkGroupFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        type = (int) getArguments().getSerializable("type");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    public void onEvent(PkGroupCreateEvent event) {
        if (type == 0) {
            addFrist(event.pkGroup);
        }
    }


    /**
     * 创建热门framgent
     *
     * @return
     */
    public static HotPkGroupFragment createHotGroupFragment() {
        HotPkGroupFragment fragment = new HotPkGroupFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", 1);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<PkGroup>>(new TypeReference<Result<List<PkGroup>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();

            }

            @Override
            public void responseInfo(Result<List<PkGroup>> data) {
                addResult(data);
            }
        });
    }

    /**
     * 创建groupfragment
     *
     * @return
     */
    public static HotPkGroupFragment createGroupFragment() {
        HotPkGroupFragment fragment = new HotPkGroupFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", 0);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 跳转到视频详细列表页面
     *
     * @param pkGroup
     */
    public void forwardDetailList(PkGroup pkGroup) {
        Intent intent = new Intent(getActivity(), PkVideoListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("pkgroup", pkGroup);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        return null;
    }

    @Override
    protected HotPkGroupViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_hot_pkgroup, parent, false);
        HotPkGroupViewHolder viewHolder = new HotPkGroupViewHolder(view, new PkItemListener() {
            @Override
            public void pkItemClick(PkGroup group) {
                //点击进入pk详情
                forwardDetailList(group);
            }
        });
        return viewHolder;
    }

    @Override
    protected HotPkGroupViewHolder createfootViewHolder(View view) {

        return new HotPkGroupViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<PkGroup> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_pk_group_list, type, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return 0;
    }

    public interface PkItemListener {
        public void pkItemClick(PkGroup group);
    }


    public static class HotPkGroupViewHolder extends BaseRecycleViewHolder<PkGroup> implements View.OnClickListener {

        private TextView mDesc;
        private TextView mJoinNum;
        private RoundNetworkImageView mUserAvatar;
        private TextView mLastTime;
        public PkItemListener pkListener;
        private View itemView;

        public HotPkGroupViewHolder(View itemView, PkItemListener listener) {
            this(itemView);
            this.pkListener = listener;
            initWidget(itemView);
        }

        public void initWidget(View itemView) {
            this.itemView = itemView;
            mDesc = (TextView) itemView.findViewById(R.id.tv_desc);
            mJoinNum = (TextView) itemView.findViewById(R.id.tv_joinnum);
            //mUserAvatar = (RoundNetworkImageView) itemView.findViewById(R.id.iv_user_avatar);
            mLastTime = (TextView) itemView.findViewById(R.id.tv_last_time);

        }

        public HotPkGroupViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void renderView(final PkGroup data, int position) {
            if (data == null) return;
            if (itemView != null)
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pkListener.pkItemClick(data);
                    }
                });
            mDesc.setText(data.description);
            mJoinNum.setText(data.cnum + "条内容");
            //  mUserAvatar.setImageUrl(data.user.avatar, HttpUtils.getAvatarImageLoader());
            if (data.lasttime != null && data.lasttime != 0)
                mLastTime.setText(DateUtil.getInterval(data.lasttime));


        }

        @Override
        public void onClick(View v) {

        }
    }


}
